/** *******************************************************************
 * Amfphp Redux
 * @author  Jonnie Spratley
 * @see jonniespratley@gmail.com
 * @internal
 ******************************************************************* */
package com.jonniespratley.amfphpredux.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.jonniespratley.amfphpredux.business.AmfphpReduxServiceDelegate;
	import com.jonniespratley.amfphpredux.events.GetServicesEvent;
	import com.jonniespratley.amfphpredux.model.ModelLocator;
	import com.jonniespratley.amfphpredux.vo.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ArrayUtil;

	/**
	 * For every feature or use case in your application, a corresponding custom command
	 * and event must be created. Each command contains an execute() method that is run
	 * by the framework when the user action has been executed.
	 * These classes are occasionally referred to as worker classes because they carry
	 * out the work for an application.
	 *
	 */
	public class GetServicesCommand implements ICommand, IResponder
	{
		private var model:ModelLocator = ModelLocator.getInstance();

		public function execute( event:CairngormEvent ) : void
		{
			var evt:GetServicesEvent = event as GetServicesEvent;
			var service:AmfphpReduxServiceDelegate = new AmfphpReduxServiceDelegate( this );
				service.getServices();
				
			trace( "Getting Services" );
		}

		public function result( data:Object ) : void
		{
			model.serviceCollection = new ArrayCollection( ArrayUtil.toArray( data.result ) );
		}

		public function fault( fault:Object ):void
		{
			var faultEvt:FaultEvent = fault as FaultEvent;
			Alert.show(faultEvt.fault.toString(), "Error");
			trace(faultEvt.fault.faultString);
		}

	}
}
